<template>
  <div class="app-container">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="150px" style="width: 400px; margin-left:50px;">
        <el-form-item label="a2dept_id" prop="a2deptId">
        <el-input v-model="temp.a2deptId" />
      </el-form-item>
      <el-form-item label="name" prop="name">
        <el-input v-model="temp.name" />
      </el-form-item>
      <el-form-item label="删除标志" prop="deleteflag">
        <el-select  v-model="temp.deleteflag" placeholder="请选择">
          <el-option
            v-for="item in deleteflagList"
            :key="item.keyName"
            :label="item.valueName"
            :value="item.keyName"
          />
        </el-select>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  createApi, updateApi,
  select4enums
} from './api'

export default {
    name: 'A2deptAdd',

    props: {
        isFromFather: {
            type: Boolean,
            default: false
        },
        row: {}
    },

    data() {
        return {
            rules: {
                a2deptId: [{ required: true, message: 'required', trigger: 'blur' }],
                name: [{ required: true, message: 'required', trigger: 'blur' }],
                deleteflag: [{ required: true, message: 'required', trigger: 'blur' }],
            },
            temp: {},
            deleteflagList: [],
            listLoading: true
        }
    },

    watch: {
        'row': {
            immediate: true, // vue首次赋值不触发watch的解决方法
            handler: function(nval){
                if (this.isFromFather) {
                    this.temp = Object.assign({}, this.row) // copy obj
                }
            }
        }
    },

    mounted() {
        this.initSelectData()
    },

    methods: {
        initSelectData() {
            // 枚举取值
            select4enums({ enumName: 'Deleteflag' }).then(resp => {
                this.deleteflagList = resp.data
                setTimeout(() => {
                    this.temp.deleteflag = resp.data[0].keyName
                })
            })
        },
        resetTemp() {
            this.temp = {
                a2deptId: '',
                name: '',
                deleteflag: '',
                id: undefined
            }
        },
        // 树表： 拿当前节点ID作为父节点ID，建立树关系
        setParentNodeId(id) {
            this.temp.a2deptId = id
        },
        createData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const param = Object.assign({}, this.temp)
                    createApi(param).then((result) => {
                        if (result.success) {
                            this.resetTemp()
                            this.$notify({
                                title: '成功',
                                message: '保存成功',
                                type: 'success',
                                duration: 2000
                            })
                            // 从父组件调用此方法，进行回调下
                            if (this.isFromFather) {
                                this.$emit('createResult')
                            }
                        }
                    })
                }
            })
        },
        updateData() {
            this.$refs['dataForm'].validate((valid) => {
                if (valid) {
                    const param = { id:this.temp.id }
                    param.a2deptId = this.temp.a2deptId
                    param.name = this.temp.name
                    param.deleteflag = this.temp.deleteflag
                    updateApi(param).then((result) => {
                        if (result.success) {
                            this.resetTemp()
                            this.$notify({
                                title: '成功',
                                message: '更新成功',
                                type: 'success',
                                duration: 2000
                            })
                            // 从父组件调用此方法，进行回调下
                            if (this.isFromFather) {
                                this.$emit('updateResult')
                            }
                        }
                    })
                }
            })
        },
        clearValidate() {
            this.$refs['dataForm'].clearValidate()
        }
    }

}
</script>
<style>

</style>
